<template>
	<view class="setting">
		<view class="information">
			<view class="title ml-20">基本信息</view>
			<u-field v-model="dataForm.name" required label="名称" input-align='right' placeholder="请输入名称"></u-field>
			<u-field v-model="dataForm.contacts" required label="联系人" input-align='right' placeholder="请输入联系人">
			</u-field>
			<u-field v-model="dataForm.phone" required label="手机号码" input-align='right' placeholder="请输入手机号码"></u-field>
			<u-field v-model="dataForm.address" label="商家地址" input-align='right' placeholder="请输入商家地址"></u-field>
			<u-field v-model="dataForm.fk_users_id" label="绑定会员" input-align='right' placeholder="请输入会员ID"></u-field>
		</view>

		<view class="information">
			<view class="title ml-20">银行卡信息</view>
			<u-field v-model="dataForm.payee" required label="收款人" input-align='right' placeholder="请输入收款人">
			</u-field>
			<u-field v-model="dataForm.bank_account" required label="银行账号" input-align='right' placeholder="请输入银行账号">
			</u-field>
			<u-field v-model="dataForm.bank" required label="收款银行" input-align='right' placeholder="请输入收款银行"></u-field>
		</view>


		<view class="information">
			<view class="title ml-20">修改密码</view>
			<u-field v-model="passForm.oldpassword" password label="旧密码" input-align='right' placeholder="请输入旧密码">
			</u-field>
			<u-field v-model="passForm.newpassword" password label="新密码" input-align='right' placeholder="请输入新密码">
			</u-field>
			<u-field v-model="passForm.againnew" password label="再次输入" input-align='right' placeholder="请再输入"></u-field>
		</view>


		<view class="save" @click="submit">保存</view>
	</view>
</template>

<script>
	import {
		GetQueryDataList,
		adminPost,
		UpdateModifyData
	} from '@/common/http.request.js'
	export default {
		data() {
			return {
				dataForm: {
					phone: '',
					address: '',
					name: '',
					contacts: '',
					fk_users_id: '',
					payee: '', //收款人
					bank_account: '', //银行账号
					bank: '', //收款银行
				},
				passForm: {
					oldpassword: '',
					newpassword: '',
					againnew: ''
				},

			}
		},
		onLoad() {
			GetQueryDataList('b_product_supplier', 1, 1, {
				pk_id: this.$store.state.vuex_adminuser.supplier_id
			}, {
				setting: false,
				api: false
			}).then(res => {
				this.dataForm = res.data[0]
			})

		},
		methods: {


			async submit() {
				let vuex_adminuser = JSON.parse(JSON.stringify(this.vuex_adminuser))
				let oldpassword = (await this.$adminPost('GetMD5Encryption', {
					password: this.passForm.oldpassword
				})).data
				let newpw = (await this.$adminPost('GetMD5Encryption', {
					password: this.passForm.newpassword
				})).data

				if (!this.dataForm.phone || this.dataForm.phone.trim() == '') {
					this.$toast('手机号不能为空')
					return
				}

				if (this.dataForm.payee.trim() == '') {
					this.$toast('收款人不能为空')
					return
				}
				
				if (!/^[0-9]*$/.test(this.dataForm.bank_account)) {
					this.$toast('收款人账号类型为数字')
					return
				}
				if (this.dataForm.bank.trim() == '') {
					this.$toast('收款人银行不能为空')
					return
				}


				if (this.passForm.oldpassword) {
					if (!(this.passForm.newpassword == this.passForm.againnew)) {
						return this.$toast('两次密码不一致')
					} else if (oldpassword != this.dataForm.password) {
						return this.$toast('旧密码不正确')
					} else if (this.passForm.newpassword.length < 6 && this.passForm.newpassword || this.passForm
						.againnew.length < 6 && this.passForm.againnew) {
						return this.$toast('密码不能少于6位')
					} else if (newpw == this.dataForm.password) {
						return this.$toast('新密码不能与旧密码相同')
					} else if (this.passForm.oldpassword.trim() == '' || this.passForm.againnew.trim() == '') {
						return this.$toast('新密码不能为空')
					}
					Object.assign(this.dataForm, {
						password: newpw
					})
				}

				UpdateModifyData('b_product_supplier', {
					...this.dataForm,
				}).then(res => {
					Object.assign(vuex_adminuser, this.dataForm)
					this.$u.vuex('vuex_adminuser', vuex_adminuser);
					this.$toast('设置成功')
					setTimeout(() => {
						this.uniBack(1)
					}, 1000)
				})

			}
		}
	}
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>

<style lang='scss' scoped>
	.setting {
		padding: 0 30rpx;

		.ml-20 {
			margin-left: 20rpx;
		}

		.information {
			width: 100%;
			background-color: #fff;
			border-radius: 20rpx;
			margin: 30rpx 0;
			padding: 20rpx;

			.title {
				font-weight: bold;
				font-size: 32rpx;
			}
		}

		.box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;

			.box-tit {}

			.box-swit {
				display: flex;
				align-items: center;
			}
		}

		.save {
			width: 100%;
			line-height: 80rpx;
			text-align: center;
			background-color: #49a3f1;
			color: #fff;
			border-radius: 20rpx;
			margin-bottom: 40rpx;
		}
	}
</style>
